#!/usr/bin/env bash
deviceId=$1

if [ $# != 1 ]; then
	echo "sh apk-launcher-time.sh deviceId"
	exit 1;
fi

currentDir=$(cd "$(dirname "$0")"; pwd)
adb -s ${deviceId} shell logcat -c
adb -s ${deviceId} shell logcat -v time -b events | grep "activity_launch_time" > ${currentDir}/adb.log &
adb -s ${deviceId} shell am start -n com.meilishuo/com.meilishuo.app.activity.WelcomeActivity

echo "sleep 10s"
sleep 10
echo "kill log colletcing script"
ps -ef | grep logcat | grep -v grep | awk '{print $2}' | xargs kill -9

grep "com.meilishuo/.app.activity.WelcomeActivity"  ${currentDir}/adb.log  > ${currentDir}/1.log
grep "com.meilishuo/.app.activity.MainActivity" ${currentDir}/adb.log > ${currentDir}/2.log

result1=`sed -n '$p' ${currentDir}/1.log  | awk -F ',' '{print $5}'`
result2=`sed -n '$p' ${currentDir}/2.log | awk -F ',' '{print $5}'`

echo ${result1%%]*} > ${currentDir}/launcher-time.txt
echo ${result2%%]*} >> ${currentDir}/launcher-time.txt
